package com.maddox.relax;

import android.text.TextUtils;

import androidx.fragment.app.FragmentActivity;

import com.maddox.relax.dialog.PhotoPickerDialog;
import com.maddox.relax.helper.CameraCompat;
import com.maddox.relax.helper.MediaStorageSource;
import com.maddox.relax.listener.OnImageTakeCompleteListener;
import com.maddox.relax.model.ImageInfo;
import com.maddox.relax.model.ImageSet;
import com.maddox.relax.util.AppUtils;
import com.maddox.relax.util.PermissionUtils;

import java.util.ArrayList;

import top.zibin.luban.Luban;


/**
 * 拍照 选择图片发起类
 */
public class PhotoPicker {

    //存储权限
    public static final int REQ_STORAGE = 995;
    //相机权限
    public static final int REQ_CAMERA = 996;


    /**
     * @param activity         调起拍照的页面activity
     * @param fileName         拍照的文件命名
     * @param completeListener 拍照结束的返回监听
     */
    public static void takePhoto(FragmentActivity activity, String fileName, OnImageTakeCompleteListener completeListener) {
        if (TextUtils.isEmpty(fileName))
            try {
                throw new Exception("照片名字不能为空,如需自动生成,请调用另一个重载方法");
            } catch (Exception e) {
                e.printStackTrace();
            }

        CameraCompat.takePhoto(activity, fileName, completeListener);


    }

    /**
     * 当前调用照片名根据时间戳自己生成
     *
     * @param activity         调起拍照的页面activity
     * @param completeListener 拍照结束的返回监听
     */
    public static void takePhoto(FragmentActivity activity, OnImageTakeCompleteListener completeListener) {
        String imageName = "IMG_" + System.currentTimeMillis() + ".jpg";
        takePhoto(activity, imageName, completeListener);
    }


    /**
     * 从相册中选择所需要的文件
     *
     * @param activity         调起相册的页面 activity
     * @param completeListener 选择文件后的回调
     * @param isMultiple       是否是多选
     */
    public static void takePhotoAlbum(FragmentActivity activity, boolean isMultiple, OnImageTakeCompleteListener completeListener) {
        takePhotoAlbum(activity, ImageSet.TYPE_ALL_IMAGE, isMultiple, completeListener);
    }


    /**
     * 从相册中选择所需要的文件
     *
     * @param activity         调起相册的页面 activity
     * @param completeListener 选择文件后的回调
     * @param mimeType         筛选的类型 分为全部  图片 视频
     * @param isMultiple       是否是多选
     */
    public static void takePhotoAlbum(FragmentActivity activity, int mimeType, boolean isMultiple, OnImageTakeCompleteListener completeListener) {
        if (PermissionUtils.hasStoragePermission(activity)) {
            MediaStorageSource.create(activity).setMediaType(mimeType).loadMediaDatas(new MediaStorageSource.MediaItemPreLoadProvider() {
                @Override
                public void providerMediaItems(ArrayList<ImageInfo> imageInfos, int mediaType) {
                    //拿到所有数据,跳转到加载列表进行展示
                    PhotoPickerDialog pickerDialog = new PhotoPickerDialog(activity);
                    pickerDialog.show();
                    pickerDialog.setMediaType(mediaType);
                    pickerDialog.setIsMultiple(isMultiple);
                    pickerDialog.setData(imageInfos);
                    pickerDialog.setOnImagesConfirmListener(completeListener);

                }
            });
        }

    }

    /**
     * 调用手机录制视频
     *
     * @param activity         调起的页面activity
     * @param completeListener 录制监听
     */
    public static void takeVideo(FragmentActivity activity, OnImageTakeCompleteListener completeListener) {
        String fileName = "Video_" + AppUtils.getFormatDate("yyyy_MM_dd_HH_mm_ss") + ".mp4";
       CameraCompat.takeVideo(activity,fileName,completeListener);
    }


}
